var request = require("../../utils/request.js")

Page({
  data: {
    phone: '',
    userInfo: {},
    step: '2',
    verifText: '获取验证码',
    isAgree: false, //是否同意协议
    isVerif: true, //发送验证码按钮是否可点击
    mobile: '', //手机号
    verification: '', //验证码
    sbtn: false,
    userInfo: {}
  },
  onLoad() {
    this.toastDialog = this.selectComponent("#toastDialog"); //此处id和页面的 wxml中的id一致

    this.getUserProfile()
    wx.showShareMenu({ menus: ['shareAppMessage', 'shareTimeline']})
  },
/**
 * 用户点击右上角分享
 */
onShareAppMessage (res) {
  return {
    title: '迪佳极视',
    path: 'pages/index/index',
    // imageUrl:'这个是显示的图片，不写就默认当前页面的截图',
    success: function (shareTickets) {
      console.info(shareTickets + '成功');
      // 转发成功
    },
    fail: function (res) {
      console.log(res + '失败');
      // 转发失败
    },
    complete:function(res){
      // 不管成功失败都会执行
    }
  }
},
  getUserProfile(e) {
    var that = this
    // 推荐使用wx.getUserProfile获取用户信息，开发者每次通过该接口获取用户个人信息均需用户确认
    // 开发者妥善保管用户快速填写的头像昵称，避免重复弹窗
    // wx.getUserProfile({
    //   desc: '登录迪佳极视', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
    //   success: (res) => {
    //     console.log(res)
    //     var obj = {
    //       avatarUrl: res.userInfo.avatarUrl,
    //       country: res.userInfo.country,
    //       city: res.userInfo.city,
    //       province: res.userInfo.province,
    //       sex: res.userInfo.gender,
    //       nickName: res.userInfo.nickName,
    //     }
    //     that.setData({
    //       userInfo: obj
    //     })
        wx.login({
          success: function (res) {
            if (res.code) {
              //发送code 获取openid
              request.postData('xcx/fetchOpenIdByCode', {
                code: res.code
              }, function (res) {
                // success
                console.log(res)
                if (res.code == '0') {
                  wx.setStorage({
                    key: 'openid',
                    data: res.openId
                    // data:"oLf5N5wQfXYroQkn_NVIV2dJCnC8"
                  })
                  let str = 'userInfo.openId'
                  that.setData({
                    [str]: res.openId,
                    step: '2'
                  })
                }
              }, function (res) {
                // error
                that.toastDialog.showDialog({
                  code: res.code,
                  message: res.message,
                  timeout: true
                });
              });
              console.log(that.data)
            } else {
              console.log('获取用户登录态失败！' + res.errMsg)
            }
          }
        });

    //   }
    // })
  },
  gotoAgree:function(){
    console.log(111)
    wx.navigateTo({
      url: '../ys/ys',
    })
  },
  querysbtn: function () {
    console.log(this.data)
    if (!this.data.isAgree) {
      this.toastDialog.showDialog({ code: 'warinning', message: '请先勾选同意《用户软件服务协议》及《隐私政策》', totastType: 'black', timeout: true
     });
      return false
    }
  },
  //获取手机号,用户允许后调用该方法
  getPhoneNumber(e) {
    console.log(e);
    if (!this.data.isAgree) {
      this.toastDialog.showDialog({ code: 'warinning', message: '请先勾选同意《用户软件服务协议》及《隐私政策》', totastType: 'black', timeout: true });
      return false
    }
    if (e.detail.errMsg == 'getPhoneNumber:fail user deny') { //用户点击拒绝
      console.log("拒绝");
    }else if(e.detail.errMsg == 'getPhoneNumber:fail:user deny') {//用户点击拒绝
      console.log("拒绝");
    } else { //允许授权执行跳转
      let str = 'userInfo.mobile'

      this.setData({
        [str]: e.detail.code
      })
      this.gotoInitUserInfo()

    }

  },
  gotoInitUserInfo: function () {
    console.log(this.data.userInfo)
    var that = this
    that.toastDialog.showDialog({ code: 'loading', message: '加载中', timeout: false }); //loading 加载框
    request.postData('xcx/initUserInfo', that.data.userInfo, function (res) {
      that.toastDialog.hideDialog(); //关闭loading提示框
      if (res.code == 0) {
        console.log("success")
        wx.switchTab({
          url: '../index/index'
        });
      } else {
        that.toastDialog.showDialog({ code: res.code, message: res.message, timeout: true });
      }

    }, function (res) {
      console.log(res)
      that.setData({ loading: false })
      that.toastDialog.showDialog({ code: res.code, message: res.message, timeout: true });

    });
  },
  gotolinks: function () {
    var that = this
    that.setData({
      link: "https://di.plus/user_agreement_xcx.html",
      title: "用户协议",
    })
    wx.navigateTo({
      url: '../ys/ys?link=' + that.data.link + '&title=' + that.data.title,
    })
    //打开隐私协议
  },
  gotoPhone: function () {
    console.log(this.data.isAgree)
    if (this.data.isAgree) {
      wx.navigateTo({
        url: '../loginPhone/loginPhone'
      })
    } else {
      this.toastDialog.showDialog({ code: 'warinning', message: '请先勾选同意《用户软件服务协议》及《隐私政策》', totastType: 'black', timeout: true });
    }

  },

  //监听radio
  radioClick: function (event) {
    var isAgree = this.data.isAgree;
    console.log(isAgree)
    this.setData({
      "isAgree": !isAgree,
      "sbtn": !isAgree
    });
    console.log(this.data)
  },
})